/**
 * 显示消息提示框
 * @param content 提示的标题
 */
import { getDicData, getTenantId, getDriverId } from '@/utils/auth';
import { getButtonShowApi } from '@/api/me.js';
import { minus } from '../uni_modules/uview-ui/libs/function/digit';
export function toast(content) {
  uni.showToast({
    icon: 'none',
    title: content
  });
}

/**
 * 显示模态弹窗
 * @param content 提示的标题
 */
export function showConfirm(content) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '提示',
      content: content,
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        resolve(res);
      }
    });
  });
}

/**
 * 参数处理
 * @param params 参数
 */
export function tansParams(params) {
  let result = '';
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    var part = encodeURIComponent(propName) + '=';
    if (value !== null && value !== '' && typeof value !== 'undefined') {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (
            value[key] !== null &&
            value[key] !== '' &&
            typeof value[key] !== 'undefined'
          ) {
            let params = propName + '[' + key + ']';
            var subPart = encodeURIComponent(params) + '=';
            result += subPart + encodeURIComponent(value[key]) + '&';
          }
        }
      } else {
        result += part + encodeURIComponent(value) + '&';
      }
    }
  }
  return result;
}

/**
 * 字典参数处理
 * @param params 参数
 */
export function getDictByString(type) {
  let dictData = getDicData() || [];
  let data = dictData.filter((item) => item.dictType == type);
  return data;
}
/**
 * 字典参数处理
 * @param params 参数
 */
export function dictTansParams(datas, name, type) {
  let results = [];
  let data = datas || [];
  data.forEach((item) => {
    if (item.dictType == name) {
      if (type == 1) {
        results.push({
          value: Number(item.value),
          text: item.label
        });
      } else {
        results.push({
          value: item.value,
          text: item.label
        });
      }
    }
  });
  return results;
}

export function timestampToChineseDate(timestamp) {
  const date = new Date(timestamp);
  const year = date.getFullYear();
  const month =
    (date.getMonth() + 1).toString().length === 1
      ? `0${date.getMonth() + 1}`
      : date.getMonth() + 1;
  const day =
    date.getDate().toString().length === 1
      ? `0${date.getDate()}`
      : date.getDate();
  return `${year}-${month}-${day}`;
}

export function getDateFormat(date, addZero = true) {
  const dates = new Date(date);
  const year = dates.getFullYear();
  const month = dates.getMonth() + 1;
  const day = dates.getDate();
  const hours = dates.getHours();
  const minus = dates.getMinutes();
  const seconds = dates.getSeconds();
  return `${year}-${addZero ? addZeroFun(month) : month}-${
    addZero ? addZeroFun(day) : day
  } ${addZero ? addZeroFun(hours) : hours}:${
    addZero ? addZeroFun(minus) : minus
  }:${addZero ? addZeroFun(seconds) : seconds}`;
}

export function addZeroFun(num) {
  if (num < 10) {
    num = `0${num}`;
  }
  return num;
}

/**
 * 按钮权限控制
 * @param params 参数
 */
export async function getButtonShow(per) {
  let permData = [];
  let menuParam = {
    enterId: getTenantId(),
    userId: getDriverId()
  };
  let res = await getButtonShowApi(menuParam);
  if (res.code == '200') {
    permData = res.data.permissions;
    let found = permData.includes(per);
    return found;
  } else {
    return false;
  }
}

export const getCurrentDateForParam = (date) => {
  const year =
    date.getFullYear().toString().length > 1
      ? date.getFullYear()
      : `0${date.getFullYear()}`;
  const month =
    (date.getMonth() + 1).toString().length > 1
      ? date.getMonth() + 1
      : `0${date.getMonth() + 1}`;
  const day =
    date.getDate().toString().length > 1
      ? date.getDate()
      : `0${date.getDate()}`;
  return `${year}-${month}-${day}`;
};

export const getUnitConversionData = (key, value, labelMap) => {
  let label = '';
  let divisor = 1;
  const THRESHOLD = 1000000;
  if (key !== 'carNumber' && value >= THRESHOLD) {
    divisor = 10000;
    label = labelMap[key];
    const maxlength = label.length;
    const startStr = label.slice(0, maxlength - 2);
    const endStr = label.slice(maxlength - 2, maxlength);
    label = `${startStr}万${endStr}`;
  } else {
    label = labelMap[key];
  }
  return {
    label,
    value: key === 'carNumber' ? value : (value / divisor).toFixed(2)
  };
};
